package com.jiangjiang.eshop.membership.controller;

import com.google.common.collect.Lists;
import com.jiangjiang.eshop.membership.domain.DeliveryAddressEntity;
import com.jiangjiang.eshop.membership.dto.DeliveryAddressDTO;
import com.jiangjiang.eshop.membership.service.DeliveryAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName DeliveryAddressController
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/24 11:01
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/membership/delivery/address")
public class DeliveryAddressController {
    @Autowired
    private DeliveryAddressService deliveryAddressService;

    /**
     * 根据用户id查询收货地址
     * @param userAccountId 用户账户id
     * @return 查询结果
     */
    @GetMapping("/queryByUserAccountId")
    public List<DeliveryAddressDTO> queryByUserAccountId(@RequestParam("userAccountId")Long userAccountId){
        try {
            return deliveryAddressService.listByUserAccountId(userAccountId);
        }catch (Exception e){
            log.warn("DeliveryAddressController queryByUserAccountId error",e);
        }
        return Lists.newArrayList();
    }

    /**
     * 新增用户地址
     * @param dto 入参
     * @return 处理结果
     */
    @PostMapping("/saveAddress")
    public Boolean saveAddress(@RequestBody DeliveryAddressDTO dto){
        try {
          return  deliveryAddressService.saveAddress(dto);
        }catch (Exception e){
            log.warn("DeliveryAddressController saveAddress error",e);
        }
        return false;
    }


    /**
     * 更新用户地址
     * @param dto 入参
     * @return 处理结果
     */
    @PostMapping("/updateAddress")
    public Boolean updateAddress(@RequestBody DeliveryAddressDTO dto){
        try {
            return  deliveryAddressService.updateAddress(dto);
        }catch (Exception e){
            log.warn("DeliveryAddressController updateAddress error",e);
        }
        return false;
    }

    /**
     * 删除用户地址
     * @param
     * @return 处理结果
     */
    @PostMapping("/remove/{id}")
    public Boolean removeAddress(@PathVariable("id") Long id){
        try {
            return  deliveryAddressService.removeById(id);
        }catch (Exception e){
            log.warn("DeliveryAddressController removeAddress error",e);
        }
        return false;
    }

}

